package com.ebupt.migu.music.api.service.impl;

import com.ebupt.migu.music.api.entity.DeptModifyRequestEntity;
import com.ebupt.migu.music.api.mapper.DeptModifyMapper;
import com.ebupt.migu.music.api.service.DeptModifyService;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: 修改用户部门信息
 * @author: liuyangyang@ebupt.com
 * @date: 2021/4/14 10:58
 */
@Slf4j
@Service
public class DeptModifyServiceImpl implements DeptModifyService {

    @Autowired
    private DeptModifyMapper deptModifyMapper;

    /**
     * 修改用户部门信息
     *
     * @param dept
     */
    @Override
    public ResultObject deptModify(DeptModifyRequestEntity dept) {
        ResultObject res = new ResultObject();
        String loginID = dept.getLoginID();
        String newDeptName = dept.getNewDeptName();
        if (null == loginID || "".equals(loginID) || null == newDeptName || "".equals(newDeptName)) {
            res.setCode(StatusEnum.BUSINID.getCode());
            res.setMsg(StatusEnum.BUSINID.getMsg());
            return res;
        }
        approveHistory(loginID, newDeptName);
        authUser(loginID, newDeptName);
        autoTriTac(loginID, newDeptName);
        autoTriTacApprove(loginID, newDeptName);
        dpiStrategy(loginID, newDeptName);
        msgType(loginID, newDeptName);
        roster(loginID, newDeptName);
        singerBlackList(loginID, newDeptName);
        smsTemplate(loginID, newDeptName);
        strategyInfo(loginID, newDeptName);
        sysUser(loginID, newDeptName);
        userApprove(loginID, newDeptName);
        userGroupInfo(loginID, newDeptName);
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        return res;
    }

    /**
     * 修改审批历史表
     *
     * @param loginID
     * @param newDeptName
     */
    private void approveHistory(String loginID, String newDeptName) {
        try {
            deptModifyMapper.approveHistory(loginID, newDeptName);
            log.info("审批历史表修改成功");
        } catch (Exception e) {
            log.error("审批历史表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改用户注册表
     *
     * @param loginID
     */
    private void authUser(String loginID, String newDeptName) {
        try {
            String deptID = queryDeptID(newDeptName);
            deptModifyMapper.authUser(loginID, deptID);
            log.info("用户注册表修改成功");
        } catch (Exception e) {
            log.error("用户注册表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改自动触发表
     *
     * @param loginID
     * @param newDeptName
     */
    private void autoTriTac(String loginID, String newDeptName) {
        try {
            deptModifyMapper.autoTriTac(loginID, newDeptName);
            log.info("自动触发表修改成功");
        } catch (Exception e) {
            log.error("自动触发表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改自动触发审批表
     *
     * @param loginID
     * @param newDeptName
     */
    private void autoTriTacApprove(String loginID, String newDeptName) {
        try {
            deptModifyMapper.autoTriTacApprove(loginID, newDeptName);
            log.info("自动触发审批表修改成功");
        } catch (Exception e) {
            log.error("自动触发审批表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改分省策略表
     *
     * @param loginID
     * @param newDeptName
     */
    private void dpiStrategy(String loginID, String newDeptName) {
        try {
            deptModifyMapper.dpiStrategy(loginID, newDeptName);
            log.info("分省策略表修改成功");
        } catch (Exception e) {
            log.error("分省策略表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改消息类型表
     *
     * @param loginID
     * @param newDeptName
     */
    private void msgType(String loginID, String newDeptName) {
        try {
            deptModifyMapper.msgType(loginID, newDeptName);
            log.info("消息类型表修改成功");
        } catch (Exception e) {
            log.error("消息类型表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改黑白名单表
     *
     * @param loginID
     * @param newDeptName
     */
    private void roster(String loginID, String newDeptName) {
        try {
            deptModifyMapper.roster(loginID, newDeptName);
            log.info("黑白名单表修改成功");
        } catch (Exception e) {
            log.error("黑白名单表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改歌手黑名单表
     *
     * @param loginID
     * @param newDeptName
     */
    private void singerBlackList(String loginID, String newDeptName) {
        try {
            deptModifyMapper.singerBlackList(loginID, newDeptName);
            log.info("歌手黑白名单表修改成功");
        } catch (Exception e) {
            log.error("歌手黑白名单表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改短信模板表
     *
     * @param loginID
     * @param newDeptName
     */
    private void smsTemplate(String loginID, String newDeptName) {
        try {
            deptModifyMapper.smsTemplate(loginID, newDeptName);
            log.info("短信模板表修改成功");
        } catch (Exception e) {
            log.error("短信模板表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改策略信息表
     *
     * @param loginID
     * @param newDeptName
     */
    private void strategyInfo(String loginID, String newDeptName) {
        try {
            deptModifyMapper.strategyInfo(loginID, newDeptName);
            log.info("策略信息表修改成功");
        } catch (Exception e) {
            log.error("策略信息表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改系统用户表
     *
     * @param loginID
     * @param newDeptName
     */
    private void sysUser(String loginID, String newDeptName) {
        try {
            String deptID = queryDeptID(newDeptName);
            deptModifyMapper.sysUser(loginID, newDeptName, deptID);
            log.info("系统用户表修改成功");
        } catch (Exception e) {
            log.error("系统用户表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改用户审批表
     *
     * @param loginID
     * @param newDeptName
     */
    private void userApprove(String loginID, String newDeptName) {
        try {
            deptModifyMapper.userApprove(loginID, newDeptName);
            log.info("用户审批表修改成功");
        } catch (Exception e) {
            log.error("用户审批表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 修改用户信息表
     *
     * @param loginID
     * @param newDeptName
     */
    private void userGroupInfo(String loginID, String newDeptName) {
        try {
            deptModifyMapper.userGroupInfo(loginID, newDeptName);
            log.info("用户信息表修改成功");
        } catch (Exception e) {
            log.error("用户信息表修改失败：{}", e.getMessage());
        }
    }

    /**
     * 查询部门ID
     *
     * @param deptName
     * @return
     */
    private String queryDeptID(String deptName) {
        try {
            return deptModifyMapper.queryDeptID(deptName);
        } catch (Exception e) {
            log.error("用户信息表修改失败：{}", e.getMessage());
            return null;
        }
    }
}
